package insight.jsp.custom.taglib;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.io.IOUtils;

public class AttributeTagHandler extends TagSupport {
	private static final long serialVersionUID = 1L;
	private String id;
	private String type;
	private String required;
	private String title;
	private String style;
	private String code;

	@Override
	public int doStartTag() throws JspException {
		try {
			JspWriter out = pageContext.getOut();
			String strOut = buildOutput();
			out.println(strOut);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_BODY_INCLUDE;
	}

	@Override
	public int doEndTag() throws JspException {
		try {
			JspWriter out = pageContext.getOut();
			String strOut = "";
			if (type != null && type.equalsIgnoreCase("combo")) {
				strOut = "</select>";
			}
			strOut += "</td></tr>";
			out.println(strOut);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_PAGE;
	}

	private String buildOutput() {
		String strOut = "<tr style=\"\"><td><b>" + title + ":</b></td><td>";
		if (type.equalsIgnoreCase("addNotes")) {
			strOut = "<tr style=\"\"><td><b>"
					+ title
					+ ":</b><br><a id=\"btnShowAddComments\" onclick=\"showAddComments();\">Show Existing Comments</a></td><td>";
		}
		String reqStr = required != null && required.equalsIgnoreCase("yes") ? " data-options=\"required:true\""
				: "";
		String styleStr = style != null ? " style=\"" + style + "\"" : "";
		String idStr = " name=\"" + id + "\"" + " id=\"" + id + "\"";

		if (type == null || type.equalsIgnoreCase("text")) {
			if (code == null || !code.equalsIgnoreCase("yes")) {
				strOut += "<input class=\"easyui-validatebox textbox\" type=\"text\""
						+ idStr + reqStr + styleStr + "></input>";
			} else {
				strOut += readFromFile("codetag.inc", styleStr, reqStr);
			}
		} else if (type.equalsIgnoreCase("textarea")) {
			strOut += "<textarea" + idStr + styleStr + "></textarea>";
		} else if (type.equalsIgnoreCase("combo")) {
			strOut += "<select class=\"easyui-combobox\" " + idStr
					+ " data-options=\"editable:false\">";
		} else if (type.equalsIgnoreCase("date")) {
			strOut += "<input class=\"easyui-datebox\" " + idStr + reqStr
					+ styleStr + "></input>";
		} else if (type.equalsIgnoreCase("number")) {
			strOut += "<input class=\"easyui-numberbox\""
					+ "data-options=\"precision:2,groupSeparator:','\"" + idStr
					+ reqStr + styleStr + "/>";
		} else if (type.equalsIgnoreCase("cost")) {
			strOut += readFromFile("costtag.inc", styleStr, reqStr);
		} else if (type.equalsIgnoreCase("p6code") && code != null) {
			try {
				code = URLEncoder.encode(code, "UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			strOut += "<select class=\"easyui-combotree\" data-options=\"url:'loadProjectCode.htm?code="
					+ code
					+ "'"
					+ (required != null && required == "yes" ? ", required:true"
							: "") + "\"" + styleStr + idStr + "></select>";

		} else if (type.equalsIgnoreCase("addNotes")) {
			strOut += "<textarea" + idStr + styleStr + "></textarea>";
		}
		return strOut;
	}

	public String readFromFile(String fileName, CharSequence styleStr,
			CharSequence reqStr) {
		InputStream in = this.getClass().getClassLoader()
				.getResourceAsStream(fileName);
		StringWriter writer = new StringWriter();
		try {
			IOUtils.copy(in, writer, "UTF-8");
		} catch (IOException e) {
			e.printStackTrace();
		}
		String strOut = writer.toString();
		strOut = strOut.replace("{id}", id).replace("{reqStr}", "required");
		return strOut;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getRequired() {
		return required;
	}

	public void setRequired(String required) {
		this.required = required;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getStyle() {
		return style;
	}

	public void setStyle(String style) {
		this.style = style;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}
}